package cn.com.klun.step;

import cn.com.klun.logic.SpiderAllStoInfoLogic;
import cn.com.klun.tjy.base.IStep;
import cn.com.klun.tjy.base.TaskConstants;
import cn.com.klun.tjy.common.constant.ResultEnum;
import cn.com.klun.tjy.common.util.DateUtils;
import cn.com.klun.tjy.dao.entity.TblAllStockInfo;
import cn.com.klun.tjy.dao.entity.TblSpiderExceptionInfo;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import cn.com.klun.tjy.dao.mapper.edit.TblSpiderExceptionInfoEditMapper;
import cn.com.klun.tjy.dao.mapper.ser.TblSpiderExceptionInfoSerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HandleSpiderExceptionTask implements IStep<TblSpiderExceptionInfo>{

    private static Logger logger = LoggerFactory.getLogger(HandleSpiderExceptionTask.class);

    @Autowired
    private TblSpiderExceptionInfoSerMapper tblSpiderExceptionInfoSerMapper;
    @Autowired
    private TblSpiderExceptionInfoEditMapper tblSpiderExceptionInfoEditMapper;
    @Autowired
    private SpiderAllStoInfoLogic spiderAllStoInfoLogic;

    @Override
    public List queryTaskInfo(TblTaskBasicConfig config) {
        TblSpiderExceptionInfo tblSpiderExceptionInfo = new TblSpiderExceptionInfo();

        // 设置处理区间 第二天八点之前都可以处理 之后的报警或者永久失效手动处理 代码判断 十分钟执行一次
        String currHour = DateUtils.getCurrDateTime().substring(8,10); // 2018/01/12 10:46:00
        if( TaskConstants.taskLockFlag.LIMIT_TIME <= Integer.parseInt(currHour)){
            // 处理交易日期是昨天的所有失败的订单 并发送邮件
            tblSpiderExceptionInfo.setDate(DateUtils.getCurrDate()); //异常信息表中交易日期为今天的
            tblSpiderExceptionInfo.setLockFlag(TaskConstants.taskLockFlag.LOCK_FLAG_SUCCESS);//不为成功的全部改为失败
            List<TblSpiderExceptionInfo> tblSpiderExceptionInfos = tblSpiderExceptionInfoSerMapper.queryNoSussessStock(tblSpiderExceptionInfo);
            int length = tblSpiderExceptionInfos.size();
            for (int i = 0; i < length; i++) {
                TblSpiderExceptionInfo tblSpiderExceptionInfo1 = tblSpiderExceptionInfos.get(i);
                tblSpiderExceptionInfo1.setLockFlag(TaskConstants.taskLockFlag.LOCK_FLAG_FAIL);
                int edit = tblSpiderExceptionInfoEditMapper.edit(tblSpiderExceptionInfo1);
                if(1!=edit){
                    logger.error("HandleSpiderExceptionTask.queryTaskInfo 更新状态失败");
                }
            }
            return null;
        }
        // 处理不成功的
        tblSpiderExceptionInfo.setLockFlag(TaskConstants.taskLockFlag.LOCK_FLAG_UNLOCK);// 未锁定的状态初始化状态
        tblSpiderExceptionInfo.setDate(DateUtils.getCurrDate());
        List<TblSpiderExceptionInfo> tblSpiderExceptionInfos = tblSpiderExceptionInfoSerMapper.queryList(tblSpiderExceptionInfo);
        return tblSpiderExceptionInfos;
    }

    @Override
    public void runTask(TblSpiderExceptionInfo tblSpiderExceptionInfo) {
        TblAllStockInfo tblAllStockInfo = new TblAllStockInfo();
        tblAllStockInfo.setEx(tblSpiderExceptionInfo.getEx());
        tblAllStockInfo.setStockCode(tblSpiderExceptionInfo.getStockCode());
        try {
            ResultEnum exec = spiderAllStoInfoLogic.exec(tblAllStockInfo);
            if (ResultEnum.OK.equals(exec)){//处理成功更改状态
                tblSpiderExceptionInfo.setLockFlag(TaskConstants.taskLockFlag.LOCK_FLAG_SUCCESS);
                tblSpiderExceptionInfoEditMapper.save(tblSpiderExceptionInfo);
                return;
            }
        } catch (Exception e) {
            logger.error("HandleSpiderExceptionTask.runTask处理异常信息失败");
        }

    }

    @Override
    public boolean editTaskInfo(List list) {
        return false;
    }
}
